package com.hu.zhcc.shiro.controller.dingming;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.dingming.TCheckitem;
import com.hu.zhcc.shiro.entity.dingming.TSportsItemLibrary;
import com.hu.zhcc.shiro.service.dingming.TSportsItemLibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/sportsItemLibrary")
public class TSportsItemLibraryController {
    @Autowired
    private TSportsItemLibraryService tSportsItemLibraryService;


    @GetMapping("")
    public ResponseEntity<Page<TSportsItemLibrary>> getSportsItemLibrary(@RequestParam(required = false) String code,
                                                                   Integer offset, Integer limit) {
        System.out.println("code="+code+",offset="+offset+",limit="+limit);
        Map<String, Object> paremeters = new HashMap<String, Object>();
        paremeters.put("code", code);
        Page<TSportsItemLibrary> result = tSportsItemLibraryService.listTSportsItem(paremeters, offset, limit);
        System.out.println("result="+result.getTotal());
        return ResponseEntity.ok(result);
    }

    @PostMapping("")
    public ResponseEntity<TSportsItemLibrary> saveSportsItem(@RequestBody TSportsItemLibrary tSportsItemLibrary) {
        tSportsItemLibrary.setCreateTime(new Date());
        int insert = tSportsItemLibraryService.insert(tSportsItemLibrary);
        return ResponseEntity.status(HttpStatus.CREATED).body(tSportsItemLibrary);
    }

    @GetMapping("/{id}")
    public ResponseEntity<TSportsItemLibrary> getCheckItemById(@PathVariable("id") Integer id) {
        TSportsItemLibrary sportsItem = tSportsItemLibraryService.selectByPrimaryKey(id);
        if (sportsItem != null) {
            return ResponseEntity.ok(sportsItem);
        }
        return ResponseEntity.notFound().build();
    }


    @PutMapping("")
    public ResponseEntity<Integer> updateCheckItem(@RequestBody TSportsItemLibrary tSportsItemLibrary) {
        int rows = tSportsItemLibraryService.updateByPrimaryKey(tSportsItemLibrary);
        if (rows == 0) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }
}
